
import Vuex from 'vuex'
import Vue from "vue";
import {TOKEN,NAME} from "@/store/mutations";
import {login,getInfo} from "@/api/login";
import {getFiles,getVideos,getSDKs} from "@/api/files"
import {getNode} from "@/api/mqtt";

Vue.use(Vuex);

const  store=new Vuex.Store({
    state: {
        token: '',
        name: '',
        avatar: '',
        info: '',
        role:false,
        essays:'',
        videos:'',
        sdks:{},
        node:{},
        client:{},


    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_INFO: (state, info) => {
            state.info= info
        },
        SET_NAME: (state, name) => {
            state.name= name
        },
        SET_AVATER: (state, icon) => {
            state.avatar= icon
        },
        SET_ROLE: (state, bool) => {
            state.role= bool
        },
        SET_ESSAY: (state, data) => {
            state.essays= data
        },
        SET_VIDEO: (state, data) => {
            state.videos= data
        },
        SET_SDK: (state, data) => {
            state.sdks= data
        },
        SET_NODE:(state, data) => {
            state.node= data
        },
        SET_CLIENT:(state, data) => {
            state.client= data
        },





    },
    actions: {
        //获取用户密钥
        Login ({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(response => {
                    const result = response.data;
                    //console.log(result.token)
                    Vue.ls.set(NAME, result.username,  60 * 60 * 1000);
                    Vue.ls.set(TOKEN, result.token,   60 * 60 * 1000);
                    commit('SET_TOKEN',result.token);
                    commit('SET_NAME',result.username);
                    resolve()
                }).catch(() => {
                    reject("用户登录失败")
                })
            })
        },
        // 获取用户信息.用户头像
        GetInfo ({ commit }) {
            return new Promise((resolve, reject) => {
                getInfo().then(response => {
                    const result = response.data.results[0]
                    //console.log(result)
                    commit('SET_INFO',result);
                    commit('SET_ROLE',true);
                    commit('SET_AVATER',result.avater);
                    //console.log(Vue.ls.get(TOKEN,''))
                    resolve(response)
                }).catch(error => {
                    commit('SET_ROLE',false);
                    reject(error)
                })
            })
        },


        //获取文章信息
        GetEssay({commit}){
            return new Promise((resolve, reject) => {
                getFiles().then(response => {
                    const result = response.data
                    //console.log(result.results)
                    //commit('SET_INFO',result);
                    commit('SET_ESSAY',result.results);
                    //console.log(Vue.ls.get(TOKEN,''))
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })

        },
        //获取视频资源
        GetVideo({commit}){
            return new Promise((resolve, reject) => {
                getVideos().then(response => {
                    const result = response.data
                    //console.log(result.results)
                    //commit('SET_INFO',result);
                    commit('SET_VIDEO',result.results);
                    //console.log(Vue.ls.get(TOKEN,''))
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })

        },
        //获取sdk资源
        GetSDK({commit}){
            return new Promise((resolve, reject) => {
            getSDKs().then(response => {
                const result = response.data
                //console.log(result.results)
                //commit('SET_INFO',result);
                commit('SET_SDK',result.results);
                //console.log(Vue.ls.get(TOKEN,''))
                resolve(response)
            }).catch(error => {
                reject(error)
            })
         })},
        GetNode({commit}){
            return new Promise((resolve, reject) => {
                getNode().then(response => {
                    const result = response.data;
                    //console.log(result.results)
                    //commit('SET_INFO',result);
                    commit('SET_NODE',result.data[0]);
                    //console.log(Vue.ls.get(TOKEN,''))
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })},




    },
    getters: {
        token:state => state.token,
        name:state =>state.name,
        info:state => state.info,
        essays:state => state.essays,
        videos:state => state.videos,
        sdks:state => state.sdks,
        node:state => state.node,
        client:state => state.client,


    }

});
export default store;


